// pages/shopDetails/shopDetails.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    tanIndex:"",
    noticeList:[],
    shareimg:"../../images/tb31.png", 
    jiantou_img:"../../images/tb60.png",
    spec:"默认",
    shoppic:"../../images/shopimg.png",
    indexPic:"../../images/tb35.png",
    collectPic:"../../images/tb34.png",
    kefuPic:"../../images/tb33.png",
    groupList:[],
    xing:"../../images/tb83.png",
    praise:"../../images/tb129.png",
    nopraise:"../../images/tb128.png",
    hide:true,
    commentList:[],
    show:false,
    chooseGid:0,
    price:0,
    num:1,
    comCount:0,
    collection:0,
    thumbUrl:getApp().globalData.thumbUrl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that=this;
    var id=options.id;
    var name=options.name;
    var uid=wx.getStorageSync('uid');
    wx.setNavigationBarTitle({
      title: name
    })
    var param={id:id,uid:uid};
    app.wxRequest('POST', "Index/robGoodsDetail", param, (res) => {
      console.log(res)
      var data=res.data;
      var goodsInfo=data.goodsInfo;
      var attrList=data.attrList;
      var collection=data.collection;
      if(collection==1){
        var collectPic="../../images/tb04.png";
      }else if(collection == 0){
        var collectPic="../../images/tb05.png";
      }
      that.setData({
        goodsInfo:goodsInfo,
        attrList:attrList,
        spec:attrList[0].name,
        chooseGid:attrList[0].id,
        price:attrList[0].price,
        bannerList:goodsInfo.bannerArr,
        comCount:data.comCount,
        content:that.escape2Html(goodsInfo.content),
        id:id,
        collectPic:collectPic,
        collection:collection
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  chooseAttr:function(e){
    var that=this;
    var id=e.currentTarget.dataset.id;
    var price=e.currentTarget.dataset.price;
    var name=e.currentTarget.dataset.name;
    var attrList=that.data.attrList;
    var chooseGid=that.data.chooseGid;
    var spec=that.data.spec;
    var price=that.data.price;
    for(var i in attrList){
      if(attrList[i].id == id){
        attrList[i].status=true;
        var chooseGid=attrList[i].id;
        var spec=attrList[i].name;
        var price=attrList[i].price;
      }else{
        attrList[i].status=false;
      }
    }
    that.setData({
      price:price,
      attrList:attrList,
      name:name,
      chooseGid:chooseGid,
      spec:spec,
      price:price
    })
    console.log(e)
    // var id=e.currentTarg
  },
  onClick:function(params) {
    this.setData({
      show:false
    })
  },
  returnIndex:function(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  escape2Html(str) {
    var arrEntities = {
      'lt': '<',
      'gt': '>',
      'nbsp': ' ',
      'amp': '&',
      'quot': '"'
    };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
      return arrEntities[t];
    }).replace('<section', '<div').replace('<img', '<img style="max-width:100%;height:auto" ');
  },
  showMoreComment:function(e){
    var that=this;
    var id=that.data.id;
    wx.navigateTo({
      url: '../comment/comment?id='+id+"&type=1",
    })
    console.log(e)
  },
  collect:function(){
    var that=this;
    var collection=that.data.collection;
    console.log(collection)
    if(collection == 1){
      collection = 0;
      var collectPic="../../images/tb05.png";
    }else if(collection == 0){
      collection = 1;
      var collectPic="../../images/tb04.png";
    }
    // console.log(collection)
    that.setData({
      collection:collection,
      collectPic:collectPic
    })
    var uid=wx.getStorageSync('uid');
    if(!uid){
      wx.navigateTo({
        url: '../authorization/authorization',
      })
      return false;
    }
    var id =that.data.id;
    var param={uid:uid,tid:id,type:1}
    app.wxRequest('POST', "Index/collGoods", param, (res) => {
  
    }, (err) => {
      console.log(err.errMsg)
    })
    
  },
  buyGoods:function(){
    var that=this;
    var gid=that.data.chooseGid;
    var num = that.data.num;
    wx.navigateTo({
      url: '../confirmOrder/confirmOrder?gid='+gid+"&num="+num+"&type=2",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // var 
  },
  toestimate(){
    wx.navigateTo({
      url: '/pages/estimate/estimate',
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  showPopup() {
    this.setData({ show: true });
  },
  onChange(event) {
    var that=this;
    var num=event.detail;
    that.setData({
      num:num
    })
    // console.log(event.detail);
  },
  topay(){
    var that=this;
    var chooseGid = that.data.chooseGid;
    var num=that.data.num;
    wx.navigateTo({
      url: '/pages/pay/pay?gid='+chooseGid+"&num="+num,
    })
  }
})